<?php
namespace Home\Controller;
use Home\Controller\CommonController;
class ZhanshiController extends CommonController {

    /**
      +----------------------------------------------------------
     * 首页
     *
      +----------------------------------------------------------
     */
    public function index() {
          

          $list=M('zlist')->select();
          
          $huxing=M('huxing')->order('id asc')->select();
          
          $fengge = I("get.fengge");
       $huxing1 = I("get.huxing");
       $where = "";

       if(!empty($fengge)){
          $data = M('zlist')->where("id = $fengge")->find();
          if(empty($where)){
            $where .= "a.fengge = '".$data['type_name']."'";
          }else{
            $where .= " and a.fengge = '".$data['type_name']."'";
          }
          
           
       }

       if(!empty($huxing1)){
          $data1 = M('huxing')->where("id=$huxing1")->find();
          if(empty($where)){
            $where .= "a.huxing = '".$data1['huxing']."'";
          }else{
            $where .= " and a.huxing = '".$data1['huxing']."'";
          }
       }

          $M=M('zhanshi');
            $count = $M->alias("a")->where($where)->count();




            $max_count = 12;
  
    $Page =new \Think\Page($count,$max_count);
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','末页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');
    // if($count < $max_count){
    //   $one = "1";

    // }
    $Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END% </div> ');



      $show = $Page->show();

       
       $shangpin=M('zhanshi')->alias("a")->where($where)->order("a.id desc")->limit($Page->firstRow, $Page->listRows)->select();
      // $zhanshi=M('zhanshi')->alias("a")->field("a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username")->join("zyb_user b on b.id = a.userid")->order("a.id desc")->limit($Page->firstRow, $Page->listRows)->select();
 //            $zhanshi=M('zhanshi')->select();
 // foreach ($zhanshi as $key => $value) {

 //        $list1=M('user')->field('user')->where('id='.$value['userid'])->find();
      


 //                $zhanshi[$key]['username']=$list1['user']; 
             
     
 
 //      }

     
     $this->assign('shangpin',$shangpin);
     $this->assign("fengge",$fengge);
      $this->assign('list',$list);
      $this->assign('huxing',$huxing);
      $this->assign('huxing1',$huxing1);
         $this->assign("page",$show);

      
      $this->display();

    }





    

    public function detail(){
 
          $id=I('get.id');
           $shangpin=M('zhanshi')->alias("a")->where(array("a.id"=>$id))->find();
             $shangpin['intro']=htmlspecialchars_decode($shangpin['intro']);
             $shangpin_status = 0;
        $result = M('shoucang')->where(array("userid"=>$_SESSION["userid"]))->find();
        if($result){
          $shangpin_shoucang = explode(',',$result['shangpin']);
          $is_has = false;
          foreach ($shangpin_shoucang as $key => $value) {
            if($value==$id){
              $is_has = true;
              break;
            }
          }
        }
        if($is_has){
          $shangpin_status = 1;
        }



      $pinglun=M('pinglun')->alias('a')->field('a.gid,a.uid,a.intro,a.time,b.user,b.touxiang')->join('zyb_user b on a.uid=b.id ')->where('a.gid='.$id)->select();

 
 $this->assign('pinglun',$pinglun);
        $this->assign('info',$shangpin);
        $this->assign("shangpin",$shangpin_status);
      $this->display();
    }

 public function shangpin(){


   $fengge=I('post.fengge');
   $huxing=I('post.huxing');


   $fengge1=M('zlist')->where(array('id'=>$fengge))->find();

   $shangpin=M('zhanshi')->alias("a")->field("a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username")->join("zyb_user b on b.id = a.userid")->where(array('fengge'=>$fengge1['type_name']))->select();
  echo json_encode($shangpin);




 }


  public function search(){

          $list=M('zlist')->select();
          
          $huxing=M('huxing')->order('id asc')->select();
          
          $fengge = I("get.fengge");
       $huxing1 = I("get.huxing");
       $where = "";

       if(!empty($fengge)){
          $data = M('zlist')->where("id = $fengge")->find();
          if(empty($where)){
            $where .= "a.fengge = '".$data['type_name']."'";
          }else{
            $where .= " and a.fengge = '".$data['type_name']."'";
          }
          
           
       }        

       if(!empty($huxing1)){
          $data1 = M('huxing')->where("id=$huxing1")->find();
          if(empty($where)){
            $where .= "a.huxing = '".$data1['huxing']."'";
          }else{
            $where .= " and a.huxing = '".$data1['huxing']."'";
          }
       }
       
       $title=I('post.title');
         $map['a.name'] =array('like',"%$title%");
          $map['a.fengge'] =array('like',"%$title%");
          $map['a.huxing'] =array('like',"%$title%");
          $map['_logic'] = 'or';
    $shangpin=M('shangpin')->alias("a")->field("a.id,a.name,a.fengge,a.huxing,a.area,a.price,a.intro,a.img,a.userid,b.user as username")->join("zyb_user b on b.id = a.userid")->where($map)->order("a.id desc")->select();
 
            $this->assign("fengge",$fengge);
      $this->assign('list',$list);
      $this->assign('huxing',$huxing);
      $this->assign('huxing1',$huxing1);
            $this->assign('shangpin',$shangpin);
            $this->display();
        
    }  
    

}
 
?>